<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\FenceFeedMedicine;
use think\facade\Db;

class FenceFeedMedicineLogic
{
    protected FenceFeedMedicine $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new FenceFeedMedicine();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @author: zj
     * @param int $Id
     * @param array $param
     * @time 2024/03/18 15:41:58
     * @return true
     */
    public function addEdit(int $Id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id,$Id);
            if ($exist) {
                $rule = [
                    "FenceId" => "integer|>:0",
                    "FeedWeight" => "integer|>:0",
                    "MedicineInfo" => "max:1500",
                    "WaterRate" => "integer|>=:0",
                ];
                validate($rule)->check($param);
                
                $updateData = [];
                if ($param['FenceId']) {
                    $updateData['out_fence_id'] = $param['FenceId'];
                }
                if ($param['FeedWeight']) {
                    $updateData['feed_weight'] = $param['FeedWeight'];
                }
                if ($param['MedicineInfo']) {
                    $updateData['medicine_info'] = $param['MedicineInfo'];
                }
                if (isset($param['WaterRate'])) {
                    $updateData['water_rate'] = $param['WaterRate'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id',$Id)
                    ->where('delete_time',$this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {
                $rule = [
                    "FenceId" => "require|integer|>:0",
                    "FeedWeight" => "require|integer|>:0",
                    "MedicineInfo" => "require|max:1500",
                    "WaterRate" => "require|integer|>=:0",
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'out_fence_id' => $param['FenceId'],
                    'feed_weight' => $param['FeedWeight'],
                    'medicine_info' => $param['MedicineInfo'],
                    'water_rate' => $param['WaterRate'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }

}